<?php
	class Topic_calc extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function add(){

			if($_POST){
				$post_data = $this->input->post();
				// print_r($post_data);exit;
				if(empty($post_data['calcs'])){
					show_message("/topic_calc/add", "没有算式");exit;
				}
				if(empty($post_data['book_id'])){
					show_message("/topic_calc/add", "没有选择课本");exit;
				}
				//查询课本
				$this->load->model('Subject_book_model');
				$book = $this->Subject_book_model->get_book_detail($post_data['book_id']);
				if(empty($book)){
					show_message("/topic_calc/add", "没有选择课本");exit;
				}

				$timu_main = array(
					'subject_id' => $post_data['subject_id'],
					'book_id' => $post_data['book_id'],
					'book_chapter_id' => $post_data['chapter_id'],
					'grade' => $book['grade'],
					'semester' => $book['semester'],
					'difficulty_level' => $post_data['difficulty_level'],
					'title' => '计算题',
					'add_time' =>time(),
					'status' => 1,
				);


				//添加子题目
				//拆分计算题
				$calcs = preg_split('/[;\r\n]+/s', $post_data['calcs']);
				foreach ($calcs as $key => $value) {
					if($value){
						//添加主题目
						$this->db->insert('tk_timu_main',$timu_main);
						$timu_main_id = $this->db->insert_id();//主题目id

						//添加子题目
						$timu_children = array(
							'timu_id' => $timu_main_id,
							'type' => 7,//题目类型（1：单选题，2：多选题，3：判断题，4：简答题，5：填空题，6：计算题，7：算式题，8：图形连线题，9：看图写答案，10：看图画星)
							'title' =>  $value,
							'analysis' => '',
						);

						$this->db->insert('tk_timu_children',$timu_children);
						$timu_children_id = $this->db->insert_id();//子主题目id


						//转换算式
						$suanshi = make_semiangle($value);
						if($suanshi){
							$suanshi_array = formula($suanshi);

							//添加题目内容（tk_timu_children_calc_content）
							foreach ($suanshi_array as $key => $value) {
								$val_type;
								$val=0;
								if(is_numeric($value)){
									$val_type = 1;
									$val = $value;
								}else{
									$val_type = 2;
									if($value=='+'){
										$val = 1;
									}else if($value=='-'){
										$val = 2;
									}else if($value=='*'){
										$val = 3;
									}else if($value=='/'){
										$val = 4;
									}else if($value=='('){
										$val = 5;
									}else if($value==')'){
										$val = 6;
									}
								}
								//添加题目内容（tk_timu_children_calc_content）
								$topic_calc_data = array(
									'timu_children_id' => $timu_children_id,
									'value' => $val,
									'type' => $val_type,//TYPE=1表示数字，TYPE=2表示符号【+=1，-=2，*=3，/=4，(=5，)=6】
									);
								$this->db->insert("tk_timu_children_calc_content", $topic_calc_data);
							}
						}



						//添加题目答案
						$timu_children_result = array(
							'timu_id' => $timu_main_id,
							'timu_children_id' => $timu_children_id,
							'answer_text' => eval("return {$suanshi};"),
							'is_right' => 1,
							'type' => 3,//显示类型（填空题专用字段：1、文本，2、符号，3、数字，4、算式）
							);

						$this->db->insert("tk_timu_children_result", $timu_children_result);
					}
				}

				// $topic = array(
				// 	'subject_id' => $post_data['subject_id'],
				// 	'book_id' => $post_data['book_id'],
				// 	'book_name' => $post_data['book_name'],
				// 	'chapter_id' => $post_data['chapter_id'],
				// 	'type' => $post_data['type'],
				// 	'difficulty_level' => $post_data['difficulty_level'],
				// 	'analysis' => $post_data['analysis'],
				// 	'has_sub_topic' => 0,
				// 	'add_time' => time(),
				// 	'status' => 1,
				// 	);
				//
				// //拆分计算题
				// $calcs = preg_split('/[;\r\n]+/s', $post_data['calcs']);
				// foreach ($calcs as $key => $value) {
				// 	$topic['subject_text'] = $value;
				// 	$this->db->insert("tk_topics", $topic);
				// 	$topic_id = $this->db->insert_id();//添加题目
				//
				// 	// $value = strtolower($value);
				// 	$suanshi = make_semiangle($value);
				// 	// print_r( eavl($value));
				// 	// $result=eval("return $abc;");
				// 	// print_r($abc."=".eval("return $abc;"));
				// 	// print_r("<br / >");
				// 	//添加题目答案
				// 	$topic_result = array(
				// 		'topic_id' => $topic_id,
				// 		'answer_text' => eval("return {$suanshi};"),
				// 		'is_right' => 1,
				// 		'type' => 3,//显示类型（填空题专用字段：1、文本，2、符号，3、数字，4、算式）
				// 		);
				//
				// 	$this->db->insert("tk_topic_result", $topic_result);
				// 	//添加题目算式
				// 	$topic_calc_data = array(
				// 		'topic_id' => $topic_id,
				// 		'value' => eval("return {$suanshi};"),
				// 		'calc' => $suanshi,
				// 		'type' => 1,//TYPE=1表示数字，TYPE=2表示符号【+=1，-=2，*=3，/=4，(=5，)=6】
				// 		);
				// 	$this->db->insert("tk_topics_calc", $topic_calc_data);
				// }

				header("Location:/topic/index");exit;

			}
			$this->load->model('Subject_model');
			$subject = $this->Subject_model->get_subject_all();
			$this->smarty->assign("subject", $subject);


			$this->load->model('Subject_book_model');
			$book_list = $this->Subject_book_model->get_book_list();
			$letter = array('A','B','C','D');
			$this->smarty->assign("book_list", $book_list['result']);
			$chapter_list_data = $this->get_chapter_list(27);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("action", '添加计算题');
			$this->smarty->display("topic/save_topic_calc2.html");
		}


		function get_chapter_list($subject_id){
			$this->load->model('Subject_book_model');
			$chapter_list = $this->Subject_book_model->get_chapter_list_by_book($subject_id);
			$chapter_list_data = array();
			$code = 0;
			if($chapter_list){
				foreach ($chapter_list as $key => $value) {
					if($value['parent_id'] == 0){
						$chapter_list_data[$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				foreach ($chapter_list as $key => $value) {
					if(isset($chapter_list_data[$value['parent_id']])){
						$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				if($chapter_list AND $chapter_list_data){
					foreach ($chapter_list_data as $key => $value) {
						if(isset($value['sub'])){
							foreach ($value['sub'] as $k => $v) {
								foreach ($chapter_list as $k1 => $v1) {
									if($k == $v1['parent_id']){
										$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
									}
								}
							}
						}
					}
				}
			}
			return $chapter_list_data;
		}



		// function make_semiangle($str)
		// {
		//     $arr = array('０' => '0', '１' => '1', '２' => '2', '３' => '3', '４' => '4',
		//                  '５' => '5', '６' => '6', '７' => '7', '８' => '8', '９' => '9',
		//                  'Ａ' => 'A', 'Ｂ' => 'B', 'Ｃ' => 'C', 'Ｄ' => 'D', 'Ｅ' => 'E',
		//                  'Ｆ' => 'F', 'Ｇ' => 'G', 'Ｈ' => 'H', 'Ｉ' => 'I', 'Ｊ' => 'J',
		//                  'Ｋ' => 'K', 'Ｌ' => 'L', 'Ｍ' => 'M', 'Ｎ' => 'N', 'Ｏ' => 'O',
		//                  'Ｐ' => 'P', 'Ｑ' => 'Q', 'Ｒ' => 'R', 'Ｓ' => 'S', 'Ｔ' => 'T',
		//                  'Ｕ' => 'U', 'Ｖ' => 'V', 'Ｗ' => 'W', 'Ｘ' => 'X', 'Ｙ' => 'Y',
		//                  'Ｚ' => 'Z', 'ａ' => 'a', 'ｂ' => 'b', 'ｃ' => 'c', 'ｄ' => 'd',
		//                  'ｅ' => 'e', 'ｆ' => 'f', 'ｇ' => 'g', 'ｈ' => 'h', 'ｉ' => 'i',
		//                  'ｊ' => 'j', 'ｋ' => 'k', 'ｌ' => 'l', 'ｍ' => 'm', 'ｎ' => 'n',
		//                  'ｏ' => 'o', 'ｐ' => 'p', 'ｑ' => 'q', 'ｒ' => 'r', 'ｓ' => 's',
		//                  'ｔ' => 't', 'ｕ' => 'u', 'ｖ' => 'v', 'ｗ' => 'w', 'ｘ' => 'x',
		//                  'ｙ' => 'y', 'ｚ' => 'z',
		//                  '（' => '(', '）' => ')', '〔' => '[', '〕' => ']', '【' => '[',
		//                  '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
		//                  '‘' => '[', '’' => ']', '｛' => '{', '｝' => '}', '《' => '<',
		//                  '》' => '>',
		//                  '％' => '%', '＋' => '+', '—' => '-', '－' => '-', '～' => '-',
		//                  // '：' => ':', '。' => '.', '、' => '\', '，' => '.', '、' => '.',
		//                  '；' => ';', '？' => '?', '！' => '!', '…' => '-', '‖' => '|',
		//                  '”' => '"', '’' => '`', '‘' => '`', '｜' => '|', '〃' => '"',
		//                  '　' => ' ', '＄'=>'$', '＠'=>'@', '＃'=>'#', '＾'=>'^', '＆'=>'&', '＊'=>'*');
		//     //foreach($arr as $k=>$v)
		//         //echo $v;
		//     return strtr($str, $arr);
		// }

	}
